﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Drawing;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace ServerApp
{
    public class ConTrolMouse
    {
        // sự kiện double click và rightclick , leftclick
        [DllImport("user32")]
        public static extern int SetCursorPos(int x, int y);
        private const int MOUSEEVENTF_MOVE = 0x0001; /* mouse move */
        private const int MOUSEEVENTF_LEFTDOWN = 0x0002; /* left button down */
        private const int MOUSEEVENTF_LEFTUP = 0x0004; /* left button up */
        private const int MOUSEEVENTF_RIGHTDOWN = 0x0008; /* right button down */
        private const int MOUSEEVENTF_RIGHTUP = 0x0010;
        [DllImport("user32.dll",
            CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
        public static extern void mouse_event(int dwFlags, int dx, int dy, int cButtons,
            int dwExtraInfo);
        bool kt;
        int x, y;
        IPEndPoint ipendp;
        Socket newsock;
        EndPoint tmpRemote;
        TCPservices tcpSv;
        public void controlmouse()
        {
            try
            {
                int port = 8080;
                byte[] data = new byte[1400];
                ipendp = new IPEndPoint(IPAddress.Any, port);
                newsock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
                newsock.Bind(ipendp);
                tmpRemote = (EndPoint)(ipendp);
                tcpSv = new TCPservices();
                kt = false;

                while (!kt)
                {
                    bool click = false;
                    bool rightClick = false;
                    data = new byte[1400];
                    int recv = newsock.ReceiveFrom(data, ref tmpRemote);
                    if (recv > 0)
                    {
                        string s = Encoding.ASCII.GetString(data);
                        string[] temp = s.Split('\0');
                        s = temp[0];
                        temp = s.Split(';');
                        if (temp.Count() >= 2)
                        {
                            x = (int)float.Parse(temp[0]);// lấy tọa độ x từ thiết bị
                            x = x * 2;
                            y = (int)float.Parse(temp[1]); // lấy tọa độ y từ thiết bị
                            y = y * 2;
                        }
                        else
                        {
                            x = 0;
                            y = 0;
                        }
                        if (temp.Count() > 2 || temp.Count() == 1)
                        {
                            click = true;
                            if (temp.Count() == 1)
                            {
                                rightClick = true;
                            }
                        }
                        //di chuyển nó đến xy
                        int X_Mouse = Cursor.Position.X;
                        int Y_Mouse = Cursor.Position.Y;
                        int xx = x + X_Mouse;
                        int yy = y + Y_Mouse;
                        Cursor.Position = new Point(xx, yy);
                        if (click)
                        {
                            SetCursorPos(xx, yy);
                            if (rightClick)
                            {
                                mouse_event(MOUSEEVENTF_RIGHTDOWN, X_Mouse, Y_Mouse, 0, 0);
                                mouse_event(MOUSEEVENTF_RIGHTUP, X_Mouse, Y_Mouse, 0, 0);
                            }
                            else
                            {
                                mouse_event(MOUSEEVENTF_LEFTDOWN, xx, yy, 0, 0);
                                mouse_event(MOUSEEVENTF_LEFTUP, xx, yy, 0, 0);
                            }
                        }

                    }
                }
                newsock.Close();
            }
            catch (Exception ex)
            {
                
                throw new Exception(ex.ToString());
            }
           
        }

    }
}
